{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-18T08:45:14.468617Z",
     "start_time": "2025-08-18T08:45:14.463986Z"
    }
   },
   "source": [
    "# series的创建\n",
    "import pandas as pd\n",
    "s = pd.Series([10,2,3,4,5])\n",
    "# 自定义索引\n",
    "s = pd.Series([10,2,3,4,5], index=['A', 'B', 'C', 'D', 'E'])\n",
    "# s = pd.Series([10,2,3,4,5], index=[1,2,3,4,5])\n",
    "# 定义name\n",
    "s = pd.Series([10,2,3,4,5], index=['A', 'B', 'C', 'D', 'E'], name = '月份')\n",
    "print(s)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    10\n",
      "B     2\n",
      "C     3\n",
      "D     4\n",
      "E     5\n",
      "Name: 月份, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:47:52.838386Z",
     "start_time": "2025-08-18T08:47:52.833269Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过字典来创建\n",
    "s = pd.Series({\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5})\n",
    "# print(s)\n",
    "s2 = pd.Series([10,2,3,4,5], index=['A', 'B', 'C', 'D', 'E'], name = '月份')\n",
    "s1 = pd.Series(s2,index=[\"A\",\"C\"])\n",
    "print(s1)"
   ],
   "id": "1d0388758dc5ca1b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    10\n",
      "C     3\n",
      "Name: 月份, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:48:14.386727Z",
     "start_time": "2025-08-18T08:48:14.383325Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# series的属性\n",
    "'''\n",
    "index:Series的索引对象\n",
    "values:Series的值\n",
    "dtype或dtypes\"Series的元素类型\n",
    "shape:Series的形状\n",
    "ndim:Series的维度\n",
    "size:Series的元素个数\n",
    "name:Series的名称\n",
    "loc[]  显式索引，按标签索引或切片\n",
    "iloc[]  隐式索引，按位置索引或切片\n",
    "at[]  使用标签访问单个元素\n",
    "iat[]  使用位置访问单个元素\n",
    "'''\n",
    "# print(s.index)\n",
    "# print(s.values)\n",
    "# print(s.shape,s.ndim,s.size)\n",
    "# s.name = 'test'\n",
    "# print(s.dtype,s.name)\n",
    "print(s.loc['a']) #显式索引\n",
    "print(s.iloc[0])  #隐式索引\n",
    "print(s.at['a'])\n",
    "print(s.iat[0])"
   ],
   "id": "6f9ada7f57b05aa4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "1\n",
      "1\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:48:23.983245Z",
     "start_time": "2025-08-18T08:48:23.977289Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 访问数据\n",
    "# print(s[1])\n",
    "# print(s['c'])\n",
    "# print(s)\n",
    "# print(s[s<3])\n",
    "s['f']=6\n",
    "print(s.head(2))\n",
    "print(s.tail(1))"
   ],
   "id": "d3ad335a6b8a5803",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1\n",
      "b    2\n",
      "dtype: int64\n",
      "f    6\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:49:01.215226Z",
     "start_time": "2025-08-18T08:49:01.211364Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 常见函数\n",
    "import numpy as np\n",
    "s = pd.Series([10,2,np.nan,None,3,4,5], index=['A', 'B', 'C', 'D', 'E','F','G'], name= 'data')\n",
    "print(s)"
   ],
   "id": "7b03df686507fa7b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    10.0\n",
      "B     2.0\n",
      "C     NaN\n",
      "D     NaN\n",
      "E     3.0\n",
      "F     4.0\n",
      "G     5.0\n",
      "Name: data, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:49:12.130188Z",
     "start_time": "2025-08-18T08:49:12.124177Z"
    }
   },
   "cell_type": "code",
   "source": [
    "s.head(3)  # 默认取前5行的数据\n",
    "s.tail(2)   #默认取后5行的数据"
   ],
   "id": "db9dd179afede944",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "F    4.0\n",
       "G    5.0\n",
       "Name: data, dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:49:20.476705Z",
     "start_time": "2025-08-18T08:49:20.470405Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查看所有的描述性信息\n",
    "s.describe()"
   ],
   "id": "4eed5fdaf5fdaa4c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count     5.000000\n",
       "mean      4.800000\n",
       "std       3.114482\n",
       "min       2.000000\n",
       "25%       3.000000\n",
       "50%       4.000000\n",
       "75%       5.000000\n",
       "max      10.000000\n",
       "Name: data, dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:49:31.792378Z",
     "start_time": "2025-08-18T08:49:31.789466Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取元素个数(忽略缺失值）\n",
    "print(s.count())"
   ],
   "id": "41642d625e6afdc5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:49:43.358886Z",
     "start_time": "2025-08-18T08:49:43.355356Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取索引\n",
    "print(s.keys())   # 方法\n",
    "print(s.index)   # 属性"
   ],
   "id": "31d53afe69b8770",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['A', 'B', 'C', 'D', 'E', 'F', 'G'], dtype='object')\n",
      "Index(['A', 'B', 'C', 'D', 'E', 'F', 'G'], dtype='object')\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:50:19.799187Z",
     "start_time": "2025-08-18T08:50:19.790479Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(s.isna())  #检查Series里的每一个元素是否为缺失值\n",
    "s.isna()"
   ],
   "id": "96fa35eb4930a7ba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    False\n",
      "B    False\n",
      "C     True\n",
      "D     True\n",
      "E    False\n",
      "F    False\n",
      "G    False\n",
      "Name: data, dtype: bool\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "A    False\n",
       "B    False\n",
       "C     True\n",
       "D     True\n",
       "E    False\n",
       "F    False\n",
       "G    False\n",
       "Name: data, dtype: bool"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:50:46.475803Z",
     "start_time": "2025-08-18T08:50:46.470705Z"
    }
   },
   "cell_type": "code",
   "source": "s.isin([4,5,6])  # 检查每个元素是否在参数集合中",
   "id": "95767f94a4c1f7e9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    False\n",
       "B    False\n",
       "C    False\n",
       "D    False\n",
       "E    False\n",
       "F     True\n",
       "G     True\n",
       "Name: data, dtype: bool"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:51:02.411590Z",
     "start_time": "2025-08-18T08:51:02.405554Z"
    }
   },
   "cell_type": "code",
   "source": "s.describe()",
   "id": "f1fcf158a8c85834",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count     5.000000\n",
       "mean      4.800000\n",
       "std       3.114482\n",
       "min       2.000000\n",
       "25%       3.000000\n",
       "50%       4.000000\n",
       "75%       5.000000\n",
       "max      10.000000\n",
       "Name: data, dtype: float64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:51:31.491963Z",
     "start_time": "2025-08-18T08:51:31.487838Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(s.mean())  #平均值\n",
    "print(s.sum())   #总和\n",
    "print(s.std())   #标准差\n",
    "print(s.var())   #方差\n",
    "print(s.min()) #最小值\n",
    "print(s.max())  #最大值\n",
    "print(s.median())  #中位数"
   ],
   "id": "6644a553e12950b9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.8\n",
      "24.0\n",
      "3.1144823004794877\n",
      "9.700000000000001\n",
      "2.0\n",
      "10.0\n",
      "4.0\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:51:41.720048Z",
     "start_time": "2025-08-18T08:51:41.716338Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# print(s.sort_values())\n",
    "print(s.quantile(0.8)) #分位数\n",
    "#————————————————\n",
    "#2  3   4  5   10\n",
    "#位置 4*0.8=3.2\n",
    "#值的计算  5 + （10-5）*0.2 = 6"
   ],
   "id": "e30a2f79795e7201",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.000000000000001\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:51:49.490Z",
     "start_time": "2025-08-18T08:51:49.485741Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#众数\n",
    "s['H']=4\n",
    "print(s.mode())"
   ],
   "id": "9fc6439c6f9b5d6b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    4.0\n",
      "Name: data, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:52:02.050487Z",
     "start_time": "2025-08-18T08:52:02.046115Z"
    }
   },
   "cell_type": "code",
   "source": "print(s.value_counts())  # 每个元素的计数",
   "id": "ddffbfebf884e5e1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data\n",
      "4.0     2\n",
      "10.0    1\n",
      "2.0     1\n",
      "3.0     1\n",
      "5.0     1\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:52:08.855003Z",
     "start_time": "2025-08-18T08:52:08.850987Z"
    }
   },
   "cell_type": "code",
   "source": [
    "s.drop_duplicates()  #去重\n",
    "s.unique()\n",
    "print(s.nunique()) #去重后的元素个数"
   ],
   "id": "c524ab47e499d9f9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:52:22.196095Z",
     "start_time": "2025-08-18T08:52:22.190749Z"
    }
   },
   "cell_type": "code",
   "source": [
    "'''创建一个包含10名学生数学成绩的Series，成绩范围在50-100之间。\n",
    "计算平均分、最高分、最低分，并找出高于平均分的学生人数。'''\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "np.random.seed(42)\n",
    "values = np.random.randint(50,101,10)\n",
    "indexes = []\n",
    "for i in range(1,11):\n",
    "    indexes.append('学生'+str(i))\n",
    "scores = pd.Series(values,indexes)\n",
    "# print(scores)\n",
    "print('平均分：',scores.mean())\n",
    "print('最高分：',scores.max())\n",
    "print('最低分：',scores.min())\n",
    "# 高于平均分的学生人数\n",
    "mean = scores.mean()\n",
    "print('高于平均分的学生人数:',len(scores[scores>mean]))\n",
    "print('高于平均分的学生人数:',scores[scores>mean].count())"
   ],
   "id": "5f3a36d17c8b5ce6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "平均分： 73.7\n",
      "最高分： 92\n",
      "最低分： 57\n",
      "高于平均分的学生人数: 4\n",
      "高于平均分的学生人数: 4\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:52:34.331224Z",
     "start_time": "2025-08-18T08:52:34.326290Z"
    }
   },
   "cell_type": "code",
   "source": [
    "'''温度数据统计\n",
    "给定某城市一周每天的最高温度Series，完成以下任务：\n",
    "找出温度超过30度的天数\n",
    "计算平均温度\n",
    "将温度从高到低排序\n",
    "找出温度变化最大的两天\n",
    "'''\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "temperatures = pd.Series([28, 31, 29, 32, 30, 27, 33],\n",
    "                         index=['周一', '周二', '周三', '周四', '周五', '周六', '周日'])"
   ],
   "id": "7d48e3947e196b4b",
   "outputs": [],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:52:44.299989Z",
     "start_time": "2025-08-18T08:52:44.296693Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 找出温度超过30度的天数\n",
    "n = temperatures[temperatures>30].count()\n",
    "print('超过30度的天数：',n)"
   ],
   "id": "1c0c751bbd7550fa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "超过30度的天数： 3\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:52:52.769209Z",
     "start_time": "2025-08-18T08:52:52.765600Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算平均温度\n",
    "print('平均温度：',temperatures.mean())"
   ],
   "id": "92e4252af436b5d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "平均温度： 30.0\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:53:01.379430Z",
     "start_time": "2025-08-18T08:53:01.375822Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将温度从高到低排序\n",
    "t2 = temperatures.sort_values(ascending=False)\n",
    "print('从高到低排序：',t2)"
   ],
   "id": "58cd183af23f6bb7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "从高到低排序： 周日    33\n",
      "周四    32\n",
      "周二    31\n",
      "周五    30\n",
      "周三    29\n",
      "周一    28\n",
      "周六    27\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:53:10.192359Z",
     "start_time": "2025-08-18T08:53:10.188870Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 找出温度变化最大的两天\n",
    "# 28 31 29 32 30 27 33\n",
    "# none 3 -2 3 -2 -3 6\n",
    "t3 = temperatures.diff().abs()   #计算series的变化值\n",
    "\n",
    "print('温度变化最大的两天',*(t3.sort_values(ascending=False).keys()[:2].tolist()))"
   ],
   "id": "35d40589b916169b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "温度变化最大的两天 周日 周二\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "股票价格分析\n",
    "给定某股票连续10个交易日的收盘价Series：\n",
    "计算每日收益率（当日收盘价/前日收盘价 - 1）\n",
    "找出收益率最高和最低的日期\n",
    "计算波动率（收益率的标准差）\n",
    "\n",
    "\n",
    "prices = pd.Series([102.3, 103.5, 105.1, 104.8, 106.2, 107.0, 106.5, 108.1, 109.3, 110.2], index=pd.date_range('2023-01-01', periods=10))\n",
    "'''"
   ],
   "id": "d1ee3510c0b77c6f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:53:43.736936Z",
     "start_time": "2025-08-18T08:53:43.732757Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 日期序列\n",
    "date = pd.date_range('2000-06-1',periods=60)\n",
    "print(list(date))"
   ],
   "id": "a7ddb4f383a9a673",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Timestamp('2000-06-01 00:00:00'), Timestamp('2000-06-02 00:00:00'), Timestamp('2000-06-03 00:00:00'), Timestamp('2000-06-04 00:00:00'), Timestamp('2000-06-05 00:00:00'), Timestamp('2000-06-06 00:00:00'), Timestamp('2000-06-07 00:00:00'), Timestamp('2000-06-08 00:00:00'), Timestamp('2000-06-09 00:00:00'), Timestamp('2000-06-10 00:00:00'), Timestamp('2000-06-11 00:00:00'), Timestamp('2000-06-12 00:00:00'), Timestamp('2000-06-13 00:00:00'), Timestamp('2000-06-14 00:00:00'), Timestamp('2000-06-15 00:00:00'), Timestamp('2000-06-16 00:00:00'), Timestamp('2000-06-17 00:00:00'), Timestamp('2000-06-18 00:00:00'), Timestamp('2000-06-19 00:00:00'), Timestamp('2000-06-20 00:00:00'), Timestamp('2000-06-21 00:00:00'), Timestamp('2000-06-22 00:00:00'), Timestamp('2000-06-23 00:00:00'), Timestamp('2000-06-24 00:00:00'), Timestamp('2000-06-25 00:00:00'), Timestamp('2000-06-26 00:00:00'), Timestamp('2000-06-27 00:00:00'), Timestamp('2000-06-28 00:00:00'), Timestamp('2000-06-29 00:00:00'), Timestamp('2000-06-30 00:00:00'), Timestamp('2000-07-01 00:00:00'), Timestamp('2000-07-02 00:00:00'), Timestamp('2000-07-03 00:00:00'), Timestamp('2000-07-04 00:00:00'), Timestamp('2000-07-05 00:00:00'), Timestamp('2000-07-06 00:00:00'), Timestamp('2000-07-07 00:00:00'), Timestamp('2000-07-08 00:00:00'), Timestamp('2000-07-09 00:00:00'), Timestamp('2000-07-10 00:00:00'), Timestamp('2000-07-11 00:00:00'), Timestamp('2000-07-12 00:00:00'), Timestamp('2000-07-13 00:00:00'), Timestamp('2000-07-14 00:00:00'), Timestamp('2000-07-15 00:00:00'), Timestamp('2000-07-16 00:00:00'), Timestamp('2000-07-17 00:00:00'), Timestamp('2000-07-18 00:00:00'), Timestamp('2000-07-19 00:00:00'), Timestamp('2000-07-20 00:00:00'), Timestamp('2000-07-21 00:00:00'), Timestamp('2000-07-22 00:00:00'), Timestamp('2000-07-23 00:00:00'), Timestamp('2000-07-24 00:00:00'), Timestamp('2000-07-25 00:00:00'), Timestamp('2000-07-26 00:00:00'), Timestamp('2000-07-27 00:00:00'), Timestamp('2000-07-28 00:00:00'), Timestamp('2000-07-29 00:00:00'), Timestamp('2000-07-30 00:00:00')]\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:53:45.888220Z",
     "start_time": "2025-08-18T08:53:45.884179Z"
    }
   },
   "cell_type": "code",
   "source": "prices = pd.Series([102.3, 103.5, 105.1, 104.8, 106.2, 107.0, 106.5, 108.1, 109.3, 110.2], index=pd.date_range('2023-01-01', periods=10))",
   "id": "9e5876a5dc7b9bac",
   "outputs": [],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:53:49.256019Z",
     "start_time": "2025-08-18T08:53:49.251098Z"
    }
   },
   "cell_type": "code",
   "source": "prices",
   "id": "8a969116b69fee35",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2023-01-01    102.3\n",
       "2023-01-02    103.5\n",
       "2023-01-03    105.1\n",
       "2023-01-04    104.8\n",
       "2023-01-05    106.2\n",
       "2023-01-06    107.0\n",
       "2023-01-07    106.5\n",
       "2023-01-08    108.1\n",
       "2023-01-09    109.3\n",
       "2023-01-10    110.2\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:56:54.917491Z",
     "start_time": "2025-08-18T08:56:54.913265Z"
    }
   },
   "cell_type": "code",
   "source": [
    "'''计算每日收益率（当日收盘价/前日收盘价 - 1）\n",
    "找出收益率最高和最低的日期\n",
    "计算波动率（收益率的标准差）'''\n",
    "# 计算每日收益率\n",
    "a = prices.pct_change()  #percent  103.5/102.3 - 1"
   ],
   "id": "738dba689f050dda",
   "outputs": [],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:56:56.318820Z",
     "start_time": "2025-08-18T08:56:56.315401Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 收益率最高的日期\n",
    "print(a.idxmax())\n",
    "# 收益率最低的日期\n",
    "print(a.idxmin())"
   ],
   "id": "12ec1b5f48778c66",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-01-03 00:00:00\n",
      "2023-01-07 00:00:00\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:56:58.309310Z",
     "start_time": "2025-08-18T08:56:58.306490Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 波动率\n",
    "print(a.std())"
   ],
   "id": "6e87d280a5c42131",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.007373623845361105\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''销售数据分析\n",
    "某产品过去12个月的销售量Series：\n",
    "计算季度平均销量（每3个月为一个季度）\n",
    "找出销量最高的月份\n",
    "计算月环比增长率\n",
    "找出连续增长超过2个月的月份\n",
    "\n",
    "sales = pd.Series([120, 135, 145, 160, 155, 170, 180, 175, 190, 200, 210, 220],index=pd.date_range('2022-01-01', periods=12, freq='MS'))'''"
   ],
   "id": "cab728a7f3ed4ec6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:55:55.299142Z",
     "start_time": "2025-08-18T08:55:55.295931Z"
    }
   },
   "cell_type": "code",
   "source": "a = pd.date_range('2022-01-01', periods=12, freq='MS')",
   "id": "6698f86a9d4bf971",
   "outputs": [],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:55:57.163282Z",
     "start_time": "2025-08-18T08:55:57.160256Z"
    }
   },
   "cell_type": "code",
   "source": "sales = pd.Series([120, 135, 145, 160, 155, 170, 180, 175, 190, 200, 210, 220],index=pd.date_range('2022-01-01', periods=12, freq='MS'))",
   "id": "9232fcb0f26d1a15",
   "outputs": [],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:55:58.776565Z",
     "start_time": "2025-08-18T08:55:58.772102Z"
    }
   },
   "cell_type": "code",
   "source": "sales",
   "id": "785b48c96d3d269a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2022-01-01    120\n",
       "2022-02-01    135\n",
       "2022-03-01    145\n",
       "2022-04-01    160\n",
       "2022-05-01    155\n",
       "2022-06-01    170\n",
       "2022-07-01    180\n",
       "2022-08-01    175\n",
       "2022-09-01    190\n",
       "2022-10-01    200\n",
       "2022-11-01    210\n",
       "2022-12-01    220\n",
       "Freq: MS, dtype: int64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:56:02.095055Z",
     "start_time": "2025-08-18T08:56:02.088225Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 季度的平均销量\n",
    "# (120+135+145)/3 = 400/3\n",
    "sales.resample('QS').mean()  #重新采样"
   ],
   "id": "cd8e501a08fb7f64",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2022-01-01    133.333333\n",
       "2022-04-01    161.666667\n",
       "2022-07-01    181.666667\n",
       "2022-10-01    210.000000\n",
       "Freq: QS-JAN, dtype: float64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:56:08.534738Z",
     "start_time": "2025-08-18T08:56:08.531363Z"
    }
   },
   "cell_type": "code",
   "source": "print('销量最高的月份',sales.idxmax())",
   "id": "3ea5875ea311fd19",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "销量最高的月份 2022-12-01 00:00:00\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:56:13.414455Z",
     "start_time": "2025-08-18T08:56:13.409097Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('月环比的增长率')\n",
    "sales.pct_change()"
   ],
   "id": "d3cfb7cb9862d10a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "月环比的增长率\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "2022-01-01         NaN\n",
       "2022-02-01    0.125000\n",
       "2022-03-01    0.074074\n",
       "2022-04-01    0.103448\n",
       "2022-05-01   -0.031250\n",
       "2022-06-01    0.096774\n",
       "2022-07-01    0.058824\n",
       "2022-08-01   -0.027778\n",
       "2022-09-01    0.085714\n",
       "2022-10-01    0.052632\n",
       "2022-11-01    0.050000\n",
       "2022-12-01    0.047619\n",
       "Freq: MS, dtype: float64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:56:17.561715Z",
     "start_time": "2025-08-18T08:56:17.557211Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 找出连续增长超过2个月的月份\n",
    "sales"
   ],
   "id": "a5de881ec6ac00a6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2022-01-01    120\n",
       "2022-02-01    135\n",
       "2022-03-01    145\n",
       "2022-04-01    160\n",
       "2022-05-01    155\n",
       "2022-06-01    170\n",
       "2022-07-01    180\n",
       "2022-08-01    175\n",
       "2022-09-01    190\n",
       "2022-10-01    200\n",
       "2022-11-01    210\n",
       "2022-12-01    220\n",
       "Freq: MS, dtype: int64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:56:20.756156Z",
     "start_time": "2025-08-18T08:56:20.750441Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = sales.pct_change()\n",
    "b=a>0\n",
    "b[b.rolling(3).sum()==3].keys().tolist()"
   ],
   "id": "17e174640732c57b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Timestamp('2022-04-01 00:00:00'),\n",
       " Timestamp('2022-11-01 00:00:00'),\n",
       " Timestamp('2022-12-01 00:00:00')]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''每小时销售数据分析\n",
    "某商店每小时销售额Series：\n",
    "按天重采样计算每日总销售额\n",
    "计算每天营业时间（8:00-22:00）和非营业时间的销售额比例\n",
    "找出销售额最高的3个小时'''"
   ],
   "id": "37c6630b722e8e01"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T08:54:34.350492Z",
     "start_time": "2025-08-18T08:54:34.342161Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "np.random.seed(42)\n",
    "h = pd.Series(np.random.randint(0,100,24),\n",
    "          index=pd.date_range('2025-01-01',periods=24,freq='h'))\n",
    "# 按天重采样计算每日总销售额\n",
    "day_sales = h.resample('D').sum()\n",
    "# hours_sales.sum()\n",
    "# 计算每天营业时间（8:00-22:00）和非营业时间的销售额比例\n",
    "mask =(h.index.hour>=8) & ((h.index.hour<=22))\n",
    "b = h[mask]\n",
    "n_b = h[~mask]\n",
    "print(b.sum()/n_b.sum())\n",
    "# 找出销售额最高的3个小时\n",
    "print(h.nlargest(3).keys())"
   ],
   "id": "b201ada17916f652",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.4294354838709677\n",
      "DatetimeIndex(['2025-01-01 11:00:00', '2025-01-01 01:00:00',\n",
      "               '2025-01-01 10:00:00'],\n",
      "              dtype='datetime64[ns]', freq=None)\n"
     ]
    }
   ],
   "execution_count": 27
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
